// firstPackage/pages/liveModule/videoList/videoList.js
import { getLiveList } from "../../../../service/config"
import { loginWxAction } from "../../../../service/config"
import { bindUserRelation } from "../../../../service/user_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    liveTZbg: "https://jyhimgs.cdjyh.com/ccc1.png",
    liveBXGbg: "https://jyhimgs.cdjyh.com/aaa.png",
    liveGXbg: "https://jyhimgs.cdjyh.com/bbb1.png",
    switchArr: [
      {
        title: "直播",
        key: "",
      },
      {
        title: "首页",
        key: "",
      },
    ],
    topTypeIndex: 0,
    menuTop: app.globalData.menuButton.top,
    menuHeight: app.globalData.menuButton.height,
    scrollRefreshStatus: true,//scroll下拉刷新状态:
    isBottomData: false,

    leftList: [

    ],
    searchWords: "",
    rightList: [
      // {
      //   type: 2,
      //   height: 398,
      // },
      // {
      //   type: 1,
      //   height: 470,
      // },
      // {
      //   type: 1,
      //   height: 430,
      // },
      // {
      //   type: 2,
      //   height: 398,
      // },
      // {
      //   type: 1,
      //   height: 450,
      // },

    ],
    requestData: {
      category: null,
      tag: null,//全部,
      liveStatus: null,
      words: "",
      page: 0,
      pageSize: 100,
    },
    roleType: 1,
    bindUserId: null,
    isShare: false,
    isCheckOpen:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      roleType: app.globalData.roleType,
      isCheckOpen: app.globalData.isCheckOpen
    })
    if (options.isShare == 1) {
      this.setData({
        isShare: true,
        bindUserId: options.userId
      })
      this.userWxLogin();
    } else {
      this.getLiveList();
    }

  },

  //用户登陆授权等:
  userWxLogin() {
    loginWxAction().then((res => {
      console.log("JJLog:登录返回:" + JSON.stringify(res));
      let data = res.data.data;
      if (res.code == 200) {
        app.globalData.nickName = data.nickName;
        app.globalData.avatar = data.avatar;
        app.globalData.name = data.name || "";
        app.globalData.phone = data.phone || "";
        app.globalData.isLogin = data.isLogin;
        app.globalData.userId = data.id;
        app.globalData.qrCode = data.qrCode || "";
        app.globalData.isAuthUserInfo = data.isAuthUserInfo;
        app.globalData.weChat = data.weChat || "";
        app.globalData.roleName = data.roleName || "";
        app.globalData.companyName = data.companyName || "";
        app.globalData.companyNumber = data.companyNumber || "";
        app.globalData.isBindMp = data.isBindMp;
        app.globalData.roleType = data.roleType || 1
        this.setData({
          roleType: app.globalData.roleType
        })
        //绑定挂级关系:
        if (this.data.bindUserId) {
          this.bindUserRelation();
        }
        this.getLiveList();



      }
    })).catch(err => {
      console.error("JJLog:userWxLogin", err);

    })
  },

  //绑定挂级关系:
  bindUserRelation() {
    let type = 2;

    bindUserRelation(type, this.data.bindUserId).then(res => {
      console.log("JJLog:绑定挂级关系:" + JSON.stringify(res));

    }).catch(err => {
      console.error("JJLog:bindUserRelation:", err);
    })
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },

  //顶部切换
  switchAction(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      topTypeIndex: index
    })
    if (index == 1) {
      wx.switchTab({
        url: '/pages/home/home',
      })
    }
  },

  getLiveList() {
    wx.showLoading();
    getLiveList(this.data.requestData).then(res => {
      wx.hideLoading();
      this.setData({
        scrollRefreshStatus: false
      })
      console.log("JJLog:直播列表:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          leftList: res.data || [],
        })

        // let leftList = this.separationArr(res.data, 1);
        // let rightList = this.separationArr(res.data, 0);
        // this.setData({
        //   leftList: leftList,
        //   rightList: rightList
        // })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
      })
      this.setData({
        scrollRefreshStatus: false
      })
      console.error("JJLog:getLiveList", err);
    })
  },

  //分配数据:
  separationArr(arr, index) {
    //index 为 0 就为偶数项， 1:就是奇数项
    let tempArr = [];
    if (index == 1) {
      arr.forEach((item, index) => {
        if (index % 2 == 0) {
          tempArr.push(item);
        }

      })
    } else if (index == 0) {
      arr.forEach((item, index) => {
        if (index % 2 != 0) {
          tempArr.push(item);
        }

      })
    }
    return tempArr;
  },

  // scroll下拉刷新:
  refreshDownData() {
    console.log("JJLog:下拉刷新来了=====:");
    this.setData({
      'requestData.page': 0,
      'requestData.category': null,
      isBottomData: false,
      scrollRefreshStatus: true,
    })
    this.getLiveList();

  },


  //跳转到直播详情:
  pushToLive(e) {
    let roomId = e.currentTarget.dataset.roomid || "";
    let isLive = e.currentTarget.dataset.live;
    let url = e.currentTarget.dataset.url || [];
    let id = e.currentTarget.dataset.id || ""
    let name = e.currentTarget.dataset.name || "";
    if (!isLive) {
      let videoUrl = url;
      app.globalData.videoUrl = videoUrl;
      wx.navigateTo({
        url: '/pages/videoPlay/videoPlay?id=' + id + "&name=" + name,
      })
      return;
    }
    wx.navigateTo({
      url: "plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=" + roomId
    })
  },
  changeLiveCategoryAction(e) {
    let category = e.currentTarget.dataset.index;
    this.setData({
      'requestData.category': category
    })
    this.getLiveList();
  },

  //搜索回调:
  searchAction(e) {
    this.setData({
      searchWords: e.detail.searchWord,
      'requestData.words': e.detail.searchWord,
      'requestData.page': 0,
      isBottomShow: false,
    })
    this.getLiveList();
  },
  backHomeAction() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {
    let userId = "";
    if (this.data.roleType == 7 || this.data.roleType == 9) {
      userId = app.globalData.userId
    }
    return {
      title: app.globalData.commonShareTitle,
      path: "/firstPackage/pages/liveModule/videoList/videoList?isShare=1" + "&userId=" + userId
    }
  }

})